---
sidebar_position: 3
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Get all connected devices

<Tabs className="runtime-tabs" groupId="runtime">
<TabItem value="web" label="Web">

After the user has granted permission to access the device, the next time the user visits the page, the Web app can access the device directly without asking for permission again.

</TabItem>
<TabItem value="node" label="Node.js">

Previously, we have disabled the permission system in `usb` package's WebUSB implementation, so it will return all devices that are currently connected.

</TabItem>
</Tabs>

The app can get all connected devices by calling `AdbDaemonWebUsbDeviceManager#getDevices` method. It can then connect to multiple devices at the same time, or let the user choose one device to connect to.

## Get devices

`AdbDaemonWebUsbDeviceManager#getDevices` method calls WebUSB's `USB#getDevices` method to get all connected devices.

But since `USB#getDevices` returns all connected devices, including those that are not ADB devices, `AdbDaemonWebUsbDeviceManager#getDevices` accepts a `filters` option (same as `AdbDaemonWebUsbDeviceManager#requestDevice`) to filter out incompatible devices.

```ts
interface AdbDeviceFilter {
  classCode: number;
  subclassCode: number;
  protocolCode: number;
  vendorId?: number;
  productId?: number;
  serialNumber?: string;
}

declare class AdbDaemonWebUsbDeviceManager {
  getDevices(filters?: AdbDeviceFilter[]): Promise<AdbDaemonWebUsbDevice[]>;
}
```

The `filters` option is optional and defaults to `ADB_DEFAULT_DEVICE_FILTER`, which picks common ADB devices:

```ts
export const ADB_DEFAULT_DEVICE_FILTER = {
  classCode: 0xff,
  subclassCode: 0x42,
  protocolCode: 1,
} as const satisfies AdbDeviceFilter;
```

So in many cases, you can just call `AdbDaemonWebUsbDeviceManager#getDevices` without any arguments:

```ts transpile
import { AdbDaemonWebUsbDevice } from "@yume-chan/adb-daemon-webusb";

const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices();
if (!devices.length) {
  alert("No device connected");
  return;
}

const device: AdbDaemonWebUsbDevice = devices[0];
```

## Filter connected devices

To limit which devices will be returned, you can specify multiple custom filters. Each filter is an object with the following fields:

- `classCode`: (Required) The class code of the ADB interface.
- `subclassCode`: (Required) The subclass code of the ADB interface.
- `protocolCode`: (Required) The protocol code of the ADB interface.
- `vendorId`: The USB vendor ID of the device.
- `productId`: The USB product ID of the device.
- `serialNumber`: The serial number of the device.

`classCode`, `subclassCode` and `protocolCode` fields are required to ensure the device is an ADB device. They can be different from `ADB_DEFAULT_DEVICE_FILTER` if your device has a different configuration. Tango will use these fields to select the correct interface to communicate with.

`venderId`, `productId` and `serialNumber` fields can be used to select a specific manufacturer, a specific model, or a specific device.

:::info

Some lazy manufacturers use the same `serialNumber` for all devices. So even if `serialNumber` is specified, it's still possible that multiple devices will be returned.

:::

If a filter has multiple fields, the device must match all fields to be returned. For example, this filter only allows a specific manufacturer/model:

```ts transpile
import {
  AdbDaemonWebUsbDevice,
  ADB_DEFAULT_DEVICE_FILTER,
} from "@yume-chan/adb-daemon-webusb";

const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices([
  {
    ...ADB_DEFAULT_DEVICE_FILTER,
    vendorId: 0x18d1,
    productId: 0x4ee2,
  },
]);
```

If multiple filters are specified, any device matching at least one filter will be returned. So, to allow multiple manufacturers/models:

```ts transpile
import {
  AdbDaemonWebUsbDevice,
  ADB_DEFAULT_DEVICE_FILTER,
} from "@yume-chan/adb-daemon-webusb";

const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices([
  {
    ...ADB_DEFAULT_DEVICE_FILTER,
    vendorId: 0x18d1,
    productId: 0x4ee2,
  },
  {
    ...ADB_DEFAULT_DEVICE_FILTER,
    vendorId: 0x04e8,
    productId: 0x6860,
  },
]);
```
